package com.ichengzi.audit.human.repository.mysql.utils;

import com.ichengzi.audit.human.repository.mysql.constant.Constant;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author lihn
 */
public class FileUtil {

    /**
     * 压缩目录
     * @param path 要压缩的目录路径
     */
    public static String zip(String path) {
        // 压缩后ZIP文件的路径
        String zipFilePath = deleteEndFileSeparator(path) + File.separator + Constant.CODE_ZIP_NAME;
        try {
            // 创建一个输出流，用于写入ZIP文件
            FileOutputStream fos = new FileOutputStream(zipFilePath);
            ZipOutputStream zipOut = new ZipOutputStream(fos);

            // 调用递归方法将目录中的文件压缩到ZIP文件中
            zipDirectory(path, "", zipOut);

            // 关闭ZIP输出流
            zipOut.close();
            fos.close();

            System.out.println("目录压缩完成：" + zipFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return zipFilePath;
    }

    /**
     * 删除目录
     *
     * @param path 要删除的路径
     */
    public static void deleteDirectory(String path) {
        File directory = new File(path);
        if (directory.exists()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        // 递归删除子目录
                        deleteDirectory(file.getAbsolutePath());
                    } else {
                        // 删除文件
                        if (!file.delete()) {
                            System.err.println("无法删除文件：" + file.getAbsolutePath());
                            return;
                        }
                    }
                }
            }
            // 删除空目录或子目录后的空目录
            directory.delete();
        }
    }

    /**
     * 执行压缩命令
     * @param sourceDirectory 压缩目标
     * @param parentDirectory 父目录
     * @param zipOut 输出流
     * @throws IOException io异常
     */
    private static void zipDirectory(String sourceDirectory, String parentDirectory, ZipOutputStream zipOut) throws IOException {
        File dir = new File(sourceDirectory);
        File[] files = dir.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    // 递归处理子目录
                    zipDirectory(file.getAbsolutePath(), parentDirectory + file.getName() + "/", zipOut);
                // 排除ZIP文件
                } else if (!file.getName().endsWith(".zip")) {
                    // 压缩文件
                    FileInputStream fis = new FileInputStream(file);
                    ZipEntry zipEntry = new ZipEntry(parentDirectory + file.getName());
                    zipOut.putNextEntry(zipEntry);

                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fis.read(buffer)) > 0) {
                        zipOut.write(buffer, 0, length);
                    }

                    fis.close();
                }
            }
        }
    }

    /**
     * 删除目录结尾分隔符
     * @param path 路径
     * @return 路径
     */
    private static String deleteEndFileSeparator(String path){
        String fileSeparatorWin = "\\";
        String fileSeparatorLinux = "/";
        if(path == null || "".equals(path)){
            return path;
        }
        if(path.endsWith(fileSeparatorWin) || path.endsWith(fileSeparatorLinux)){
            path = path.substring(0, path.length() - 1);
        }
        return path;
    }
}
